package org.ms.mconf.sdk.model;

import com.alibaba.fastjson.JSON;
import lombok.*;
import org.ms.mconf.sdk.Constants;

import java.io.Serializable;
import java.util.Set;

/**
 * The Data Node
 *
 * @author lry
 */
@Data
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class NodeConfig implements Serializable {

    /**
     * The node id
     */
    private String id;

    /**
     * The node name
     */
    private String name;
    /**
     * The sub node config
     */
    private Set<NodeConfig> nodes;

    public NodeConfig(String id) {
        this.id = id;
    }

    public NodeConfig(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public static String buildNodeKey(String group) {
        return String.join(Constants.DELIMITER, group, Constants.NODE.getName());
    }

    public static String buildEnvKey(String group) {
        return String.join(Constants.DELIMITER, group, Constants.ENV_KEY);
    }

    public static String buildValue(NodeConfig nodeConfig) {
        return JSON.toJSONString(nodeConfig);
    }

    public static NodeConfig parseValue(String value) {
        return JSON.parseObject(value, NodeConfig.class);
    }

}
